from django.db import models
from userapp.models import User


class Category(models.Model):
    name = models.CharField(max_length=20)
    # 自关联
    parent = models.ForeignKey("self", on_delete=models.CASCADE, null=True)


class Goods(models.Model):
    name = models.CharField(max_length=20)

    cate1 = models.IntegerField()
    cate2 = models.IntegerField()
    cate3 = models.IntegerField()

    price = models.IntegerField()
    pic = models.ImageField(upload_to="static/goods", null=True)
    desc = models.CharField(max_length=120)
    params = models.TextField('规格', null=True)


class Tag(models.Model):
    name = models.CharField(max_length=20)
    cate = models.IntegerField('属于哪个顶级分类')
    goods = models.ManyToManyField(Goods, through="Goods_Tag")


class Goods_Tag(models.Model):
    goods = models.ForeignKey(Goods, on_delete=models.SET_NULL, null=True)
    tag = models.ForeignKey(Tag, on_delete=models.SET_NULL, null=True)


class Cart(models.Model):
    goods = models.ForeignKey(Goods, on_delete=models.SET_NULL, null=True)
    num = models.IntegerField()
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)


class Address(models.Model):
    province = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    district = models.CharField(max_length=30)
    place = models.CharField(max_length=30)
    receiver = models.CharField(max_length=30)
    mobile = models.CharField(max_length=30)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)